import re
import math
import unicodedata

title = ""
content = ""
process_mode = 0  # 默认平均处理 1表示第一个后面为5个，其余的平均
title_mode = 0  # 默认title格式 xx（xx）
title_label = 'h2'
title_style = 'color:red'


def process_title(title, title_label='h2', title_style='color:red'):
    '''
    :param title: 默认title格式： 光波炉和微波炉的区别（微波炉和光波炉哪个好更实用）
    :return:
        [
            '<h2 style="color:red">光波炉和微波炉的区别</h2>',
            '<h2 style="color:red">微波炉和光波炉哪个好更实用</h2>'
        ]
    '''
    if title_mode == 0:
        title_lis = re.findall(r'([^（）]+)', title)
        return ['{}<{} style="{}">{}</{}>\n\n'.format('', title_label, title_style, item, title_label)
                for index, item in enumerate(title_lis)]


def strip_unicode(text):
    stripped_text = ""
    for char in text:
        if unicodedata.category(char)[0] != "C":
            stripped_text += char
    return stripped_text


def process_content(content):
    pattern = re.compile(r"<p>\d+、")
    results = [item for item in pattern.split(content) if len(item.strip()) > 10 or (len(strip_unicode(item)) > 0)]
    return results


def insert_title_into_content(title_list, content_list, process_mode=0):
    content_length = len(content_list)
    title_length = len(title_list)
    insert_index = []
    if process_mode == 0 or (process_mode == 1 and content_length <= 5):
        insert_index = [(i, i * math.ceil(content_length / title_length)) for i in range(title_length)]
    elif process_mode == 1:
        insert_index = [(0, 0)]
        insert_index += [(i + 1, i * (content_length - 5) / (title_length - 1) + 5) for i in range(title_length - 1)]
    result_list = []
    insert_index_lis = [i[1] for i in insert_index]
    paragraph_index = 0
    for index, item in enumerate(content_list):
        if index in insert_index_lis:
            paragraph_index = 0
            result_list.append(title_list[insert_index[insert_index_lis.index(index)][0]])
        new_content = '<p>{}、{}'.format(paragraph_index + 1, item)
        result_list.append(new_content)
        paragraph_index += 1
    return result_list


def process_file(title, content):
    title_list = process_title(title)
    content_list = process_content(content)
    result_content = insert_title_into_content(title_list, content_list, process_mode)
    return ''.join(result_content)


if __name__ == '__main__':
    # print(process_title("光波炉和微波炉的区别（微波炉和光波炉哪个好更实用）"))
    # print(insert_title_into_content(['a', 'b'], ['1', '2', '3', '4', '5', '6', '7', '8', '9'], process_mode=0))
    print(process_content("""
<p>1、也是广告策划整体方案的重要组成部分。地点使用或内容进行不同的组合运用，媒体租金方案，其中广告目标一栏广告策划又分为知名度，任务和目标内容。</p>

<p>2、这是全部计划的搞要，方案广告媒体战略，另一类是将媒体计划写成书面材料，内容电视广告策划。广告效果预测，广告预算书的格式和内容不可方案能千篇一律。</p>

<p>3、广告预算书后一般还附加一广告策划段说明文字。4广告策划。人们通常方案所说的广告策划书和广告策划书实际是一回事。</p>
"""))
